#include "cJSON.h"
#include "stdio.h"

int main(void){
    char json[]="{\"name\":\"test\",\"age\":18,\"sex\":\"man\"}";
    cJSON *root=cJSON_Parse(json);
    if(root==NULL){
        printf("json parse error\n");
    }
    //把值取出来
    cJSON *name=cJSON_GetObjectItem(root,"name");
    cJSON *age=cJSON_GetObjectItem(root,"age");
    cJSON *sex=cJSON_GetObjectItem(root,"sex");
    printf("name:%s\n",name->valuestring);
    printf("age:%d\n",age->valueint);
    printf("sex:%s\n",sex->valuestring);

    cJSON* root2 = cJSON_CreateObject();
    cJSON_AddStringToObject(root2,"name","lisi");
    cJSON_AddNumberToObject(root2,"age",20);
    char *json2_str=cJSON_PrintUnformatted(root2);
    printf("json2_str:%s\n",json2_str);

    cJSON_Delete(root);
    cJSON_Delete(root2);
    return 0;
}